package edu.xidian.bdilab.jrbigdata.service.enterprise.productRequest.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.xidian.bdilab.jrbigdata.controller.bank.BankCode;
import edu.xidian.bdilab.jrbigdata.controller.enterprise.EnterpriseCode;
import edu.xidian.bdilab.jrbigdata.controller.response.ResponseResult;
import edu.xidian.bdilab.jrbigdata.domain.EnterpriseInfo;
import edu.xidian.bdilab.jrbigdata.domain.EnterpriseReq;
import edu.xidian.bdilab.jrbigdata.domain.FinancialProduct;
import edu.xidian.bdilab.jrbigdata.domain.ProductRequest;
import edu.xidian.bdilab.jrbigdata.mapper.EnterpriseInfoMapper;
import edu.xidian.bdilab.jrbigdata.mapper.EnterpriseReqMapper;
import edu.xidian.bdilab.jrbigdata.mapper.FinancialProductMapper;
import edu.xidian.bdilab.jrbigdata.mapper.ProductRequestMapper;
import edu.xidian.bdilab.jrbigdata.service.enterprise.productRequest.ProductRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static java.lang.Boolean.TRUE;

/**
 * @ClassName ProductRequestServiceImpl
 * @Description TODO
 * @Author tuantuan
 * @Date 2019/4/2 上午10:47
 * @Version 1.0
 * @Attention Copyright (C), 2004-2019, BDILab, XiDian University
 **/
@Service
public class ProductRequestServiceImpl implements ProductRequestService {
    @Autowired
    ProductRequestMapper productRequestMapper;
    @Autowired
    EnterpriseInfoMapper enterpriseInfoMapper;
    @Autowired
    FinancialProductMapper financialProductMapper;

    @Override
    public ResponseResult setRequestInfo(String socialCode, String productName, String fundUsage) throws ParseException {
        ResponseResult responseResult = new ResponseResult();

        QueryWrapper<ProductRequest> productRequestQueryWrapper = new QueryWrapper<>();
        productRequestQueryWrapper.eq("fk_social_code", socialCode)
                                  .eq("fk_product_name", productName);
        ProductRequest oldProductRequest = productRequestMapper
                .selectOne(productRequestQueryWrapper);
        if(oldProductRequest!=null)
        {
            responseResult.setData("已经申请过对应商品");
            responseResult.setCode(EnterpriseCode.PRODUCT_REQUEST_FAIL.getCode());
            responseResult.setMessage(EnterpriseCode.PRODUCT_REQUEST_FAIL.getMessage());
            responseResult.setSuccess(false);
            return responseResult;
        }
        QueryWrapper<EnterpriseInfo> enterpriseInfoQueryWrapper = new QueryWrapper<>();
        enterpriseInfoQueryWrapper.eq("social_code", socialCode);
        EnterpriseInfo enterpriseInfo = enterpriseInfoMapper.selectOne(enterpriseInfoQueryWrapper);

        ProductRequest productRequest = new ProductRequest();

        productRequest.setFkProductName(productName);
        productRequest.setFkSocialCode(socialCode);
        productRequest.setEnterpriseName(enterpriseInfo.getEnterpriseName());
        productRequest.setStatus(0);
        productRequest.setComment("暂无进度");
        if(fundUsage!=null&&fundUsage.length()!=0)
        {   productRequest.setFundUsage(fundUsage); }
        else{
            productRequest.setFundUsage("暂未说明用途");
        }

        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String temp = dateFormat.format(date);

        productRequest.setRequestTime(dateFormat.parse(temp));
        productRequestMapper.insert(productRequest);

        responseResult.setCode(EnterpriseCode.PRODUCT_REQUEST_SUCCESS.getCode());
        responseResult.setMessage(EnterpriseCode.PRODUCT_REQUEST_SUCCESS.getMessage());
        responseResult.setSuccess(false);
        return responseResult;

    }

    @Override
    public ResponseResult getAllProductRequest(Integer pageNum,Integer pageSize,String enterpriseName,String socialCode,String productName,String bankName){
        QueryWrapper<ProductRequest> productRequestQueryWrapper= new QueryWrapper<>();
        PageHelper.startPage(pageNum,pageSize);
        productRequestQueryWrapper.like(enterpriseName!=null,"enterprise_Name",enterpriseName);
        productRequestQueryWrapper.like(socialCode!=null,"fk_social_code",socialCode);
        productRequestQueryWrapper.like(productName!=null,"fk_product_name",productName);
        List<ProductRequest> productRequestList=productRequestMapper.selectList(productRequestQueryWrapper);
        List<Map<String,String>> maps=new ArrayList<>();
        for (ProductRequest productRequest : productRequestList) {
            Map<String,String> map=new HashMap<>();
            map.put("productName",productRequest.getFkProductName());
            map.put("enterpriseName",productRequest.getEnterpriseName());
            map.put("comment",productRequest.getComment());
            map.put("fundUsage",productRequest.getFundUsage());
            map.put("requestTime",productRequest.getRequestTime().toString());
            switch (productRequest.getStatus()){
                case 0:map.put("status","该申请进展中");break;
                case 1:map.put("status","该申请已成功");break;
                case 2:map.put("status","该申请已失败");break;
            }
            QueryWrapper<FinancialProduct> financialProductQueryWrapperr=new QueryWrapper<>();
            financialProductQueryWrapperr.eq("product_name",productRequest.getFkProductName());
            financialProductQueryWrapperr.like(bankName!=null,"bank_name",bankName);
            FinancialProduct financialProduct = financialProductMapper.selectOne(financialProductQueryWrapperr);
            if(financialProduct!=null){
                map.put("bankName",financialProduct.getBankName());
                switch (financialProduct.getLoanLimit()){
                    case 0:map.put("money","100");break;
                    case 1:map.put("money","200");break;
                    case 2:map.put("money","300");break;
                    case 3:map.put("money","500");break;
                    default:map.put("money","1000");break;
                }
                maps.add(map);
            }
        }
        PageInfo pageInfo=new PageInfo<> (maps);
        ResponseResult responseResult = new ResponseResult();
        Map<String,Object> data=new HashMap<>(3);
        data.put("productRequest List",maps);
        data.put("Total Page Num",pageInfo.getPages());
        data.put("Total",pageInfo.getTotal());
        if (maps.size() == 0) {
            responseResult.setCode(BankCode.CHECK_NONE.getCode());
            responseResult.setMessage(BankCode.CHECK_NONE.getMessage());
            responseResult.setSuccess(TRUE);
        } else {
            responseResult.setData(data);
            responseResult.setCode(BankCode.CHECK_SUCCESS.getCode());
            responseResult.setMessage(BankCode.CHECK_SUCCESS.getMessage());
            responseResult.setSuccess(TRUE);
        }
        return responseResult;
    }
}
